package com.kadron.session;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;

import com.kadron.common.KUtil;
import com.kadron.dao.KCurrencyDAO;
import com.kadron.entity.Currency;
import com.kadron.entity.User;
import com.kadron.enumeration.KLang;

@Startup
@AutoCreate
@Name("kUser")
@Scope(ScopeType.SESSION)
public class KUser implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3419198193908035430L;
	
	private static final Log log = LogFactory.getLog(KUser.class);

	private User user;
	private String klang = KLang.EN.toString();
	private Currency currency;
	private Basket basket;
	private String avatar;
	
	@In
	private KCurrencyDAO currencyDAO;
	
	public String getKlang() {
		if (KUtil.isEmpty(klang)) {
			if (log.isDebugEnabled()) {
				log.debug("klang is empty it ll be ste on KLang.EN !!!");
			}
			klang = KLang.EN.toString();
		}
		return klang;
	}

	public void setKlang(String klang) {
		this.klang = klang;
	}

	@Create
	public void init() {		
		currency = currencyDAO.find("EUR");
	}
	
	public void init(User user) {
		this.user = user;
		if (user.getLang() != null) {
			this.klang = user.getLang().toString();		
		}
	}
	
	public User getUser() {
		return user;
	}
	
	public Currency getCurrency() {
		return currency;
	}

	public void setCurrency(Currency currency) {
		this.currency = currency;
	}

	public Basket getBasket() {
		if (basket == null) {
			basket = new Basket();
		}
		return basket;
	}
	
	public static KUser instance() {
		KUser kUser = (KUser) Component.getInstance("kUser");
		return kUser;
	}
	
	public static KUser instance(User user) {
		KUser kUser = (KUser) Component.getInstance("kUser");
		kUser.init(user);
		return kUser;
	}

	public String getAvatar() {
		return avatar;
	}
	
	public boolean isAdmin() {
		return user != null && user.isAdmin();
	}

}